#include "qattributeeditor.h"

#include "../shared/qdriveattribute.h"
#include "../shared/qdrivevaluemap.h"
#include "../shared/qproject.h"

QAttributeEnumEditor::QAttributeEnumEditor(QDriveAttribute *property, QWidget *parent):
    QComboBox(parent),
    m_property(property)
{

    QStringList list=m_property->attribute("Enum").toStringList();

    this->addItems(list);
    this->setCurrentIndex(this->findText(property->valueText()));
    connect(this,SIGNAL(currentIndexChanged(int)),this,SLOT(setPropertyValue(int)));
    this->setFrame(false);
}

void QAttributeEnumEditor::setPropertyValue(int value)
{
    m_property->setValue(this->itemText(value));
}

QAttributeLineEditEditor::QAttributeLineEditEditor(QDriveAttribute *property, QWidget *parent):
    QLineEdit(parent),
    m_property(property)
{
    this->setText(property->valueText());
    connect(this,SIGNAL(editingFinished()),this,SLOT(setPropertyValue()));
    this->setFrame(false);
}

void QAttributeLineEditEditor::setPropertyValue()
{
    m_property->setValue(text());
}

QAttributeIntEditor::QAttributeIntEditor(QDriveAttribute *property, QWidget *parent):
    QSpinBox(parent),
    m_property(property)
{
    this->setValue(m_property->value().toInt());
    setRange(m_property->attribute("Min",0).toInt(),m_property->attribute("Max",65535).toInt());

    connect(this,SIGNAL(editingFinished()),this,SLOT(setPropertyValue()));
    this->setFrame(false);
}

void QAttributeIntEditor::setPropertyValue()
{
    m_property->setValue(this->value());
}




QValueIntEditor::QValueIntEditor(QDriveValueMap *value, int row, int column, QWidget *parent):
    QSpinBox(parent),
    m_value(value),
    m_row(row),
    m_column(column)
{
    this->setValue(m_value->value(row,column).toInt());

    QHeaderInfo *info=value->header(column);
    if(info->property("Max").isNull())
    {
        info->setProperty("Max","65535");
    }
    setRange(info->property("Min").toInt(),info->property("Max").toInt());

    connect(this,SIGNAL(editingFinished()),this,SLOT(setPropertyValue()));
    this->setFrame(false);
}

void QValueIntEditor::setPropertyValue()
{
    m_value->setValue(m_row,m_column,QString::number(this->value()));
}


QValueEnumEditor::QValueEnumEditor(QDriveValueMap *value, int row, int column, QWidget *parent):
    QComboBox(parent),
    m_value(value),
    m_row(row),
    m_column(column)
{

    QStringList list=m_value->header(column)->property("Enum").toStringList();

    this->addItems(list);

    this->setCurrentIndex(this->findText(value->value(row,column)));
    connect(this,SIGNAL(currentIndexChanged(int)),this,SLOT(setPropertyValue(int)));
    this->setFrame(false);
}

void QValueEnumEditor::setPropertyValue(int value)
{
    m_value->setValue(m_row,m_column,this->itemText(value));
}


QValueLineEditEditor::QValueLineEditEditor(QDriveValueMap *value, int row, int column, QWidget *parent):
    QLineEdit(parent),
    m_value(value),
    m_row(row),
    m_column(column)
{
    this->setText(value->value(row,column));
    connect(this,SIGNAL(editingFinished()),this,SLOT(setPropertyValue()));
    this->setFrame(false);
}

void QValueLineEditEditor::setPropertyValue()
{
    m_value->setValue(m_row,m_column,text());
}


QValueMemeryEditor::QValueMemeryEditor(QDriveValueMap *value, int row, int column, QWidget *parent):
    QComboBox(parent),
    m_value(value),
    m_row(row),
    m_column(column)
{
    QStringList list;

    QProject *p=QProject::project();

    int count=p->getRuntimeDataCount();
    for(int i=0;i<count;i++)
    {
        if(p->getRuntimeData(i).isMemery())
        {
            list.append(p->getRuntimeData(i).name());
        }
    }

    this->addItems(list);

    this->setCurrentIndex(this->findText(value->value(row,column)));
    connect(this,SIGNAL(currentIndexChanged(int)),this,SLOT(setPropertyValue(int)));
    this->setFrame(false);
}

void QValueMemeryEditor::setPropertyValue(int value)
{
    m_value->setValue(m_row,m_column,this->itemText(value));
}


QWidget *QEditorCreator::createAttributeEditor(QDriveAttribute *attr, QWidget *parent)
{
    QString str=attr->attribute("Editor_Type").toString();
    if(str=="ComboBox")
    {
        return new QAttributeEnumEditor(attr,parent);
    }
    else if(str=="SpinBox")
    {
        return new QAttributeIntEditor(attr,parent);
    }
    else
    {
        return new QAttributeLineEditEditor(attr,parent);
    }
}

QWidget *QEditorCreator::createValueEditor(QDriveValueMap *value, QWidget *parent, int row, int column)
{
    QString str=value->header(column)->property("type").toString();
    if(str=="int")
    {
        return new QValueIntEditor(value,row,column,parent);
    }
    else if(str=="ComboBox")
    {
        return new QValueEnumEditor(value,row,column,parent);
    }
    else if(str=="Memery_Enum")
    {
        return new QValueMemeryEditor(value,row,column,parent);
    }
    else
    {
        return new QValueLineEditEditor(value,row,column,parent);
    }
}
